package com.app.couponsharelife.ui.main.special.fragment

import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.ui.listener.MyMultiPurposeListener
import com.app.couponsharelife.ui.main.home.good.GoodDetailActivity
import com.app.couponsharelife.ui.main.special.BrandSpecialListActivity
import com.app.couponsharelife.utils.LoadUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.base_refresh_head.*
import kotlinx.android.synthetic.main.fragment_brand_special.*
import kotlinx.android.synthetic.main.rvitem_brand_special.view.*
import java.util.HashMap

/**
 *Created by xct on 2019/5/17
 *Description:
 */
class BrandSpecialFragment:LazyFragment() {
    private var classBean:HomeClassBean?=null
    private var adapter:MyAdapter?=null
    private var dataList= arrayListOf<BrandSpecialData>()
    private var minId="1"
    companion object {
        fun newInstance(homeClassBean: HomeClassBean): BrandSpecialFragment {
            val fragment = BrandSpecialFragment()
            val bundle= Bundle()
            bundle.putParcelable("Data",homeClassBean)
            fragment.arguments =bundle
            return fragment
        }
    }
    override val viewLayoutId: Int
        get() = R.layout.fragment_brand_special

    override fun initViews() {
        classBean=arguments!!.getParcelable("Data")
        setRefresh()
        adapter=MyAdapter()
        rvGoods.layoutManager=LinearLayoutManager(context)
        rvGoods.adapter=adapter
    }
    private fun setRefresh(){
        hotsale_refreshimg.setImageResource(R.drawable.anim_refresh)
        val animationDrawable=hotsale_refreshimg.drawable as AnimationDrawable
        //smartrefresh.setEnableNestedScroll(true)
        smartrefresh.setOnMultiPurposeListener(object : MyMultiPurposeListener {
            override fun headerReleased() {
                animationDrawable.start()
            }

            override fun headerMoving(percent: Float) {
                setAnima(percent)
            }

            override fun headerFinish() {
                hotsale_refreshimg.setImageResource(R.drawable.refresh_0)
                animationDrawable.stop()
            }

            override fun loadMore() {
                getData(false)
            }

            override fun refresh() {
                minId="1"
                getData(true)
            }

        })
    }
    private fun setAnima(vlaue:Float){
        val layoutParams=hotsale_refreshimg.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintPercentHeight=vlaue/2
        layoutParams.dimensionRatio="0.875:1"
        hotsale_refreshimg.layoutParams=layoutParams
    }
    override fun lazyLoad() {
        getData(false)
    }
    private fun getData(isRefresh:Boolean){
        if (classBean==null){
            showToastMsg("分类数据加载出错！")
            return
        }
        if ("1"==minId){
            showProgressDialog("加载中...")
        }
        val map= HashMap<String,String>()
        map["minId"]=minId
        map["back"]="10"
        map["brandcat"]=(classBean!!.id?:0).toString()
        RetrofitWorker.retrofitWorker.getBrandListData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<BrandSpecialListBean>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<BrandSpecialListBean>) {
                        if ("1"==minId){
                            dismissProgressDialog()
                        }
                        if (isRefresh){
                            smartrefresh.finishRefresh()
                        }else{
                            if (minId!="1"){
                                smartrefresh.finishLoadMore()
                            }
                        }
                        if (minId=="1"){
                            dataList.clear()
                        }
                        if (it.code==200&&it.data!=null&&it.data!!.data!=null){
                            if (it.data!!.data!!.isNotEmpty()){
                                if (minId=="1"){
                                    llerror.visibility=View.GONE
                                    rvGoods.visibility=View.VISIBLE
                                }
                                minId=it.data!!.min_id?:""
                                dataList.addAll(it.data!!.data!!)
                                adapter!!.notifyDataSetChanged()
                            }else{
                                if (minId=="1"){
                                    llerror.visibility=View.VISIBLE
                                    rvGoods.visibility=View.GONE
                                }else{
                                    smartrefresh.finishLoadMoreWithNoMoreData()
                                }
                            }
                        }else{
                            smartrefresh.finishLoadMoreWithNoMoreData()
                            if (minId=="1"){
                                llerror.visibility=View.VISIBLE
                                rvGoods.visibility=View.GONE
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        if ("1"==minId){
                            dismissProgressDialog()
                        }
                        llerror.visibility=View.VISIBLE
                        rvGoods.visibility=View.GONE
                        if (isRefresh){
                            smartrefresh.finishRefresh()
                        }
                        smartrefresh.finishLoadMoreWithNoMoreData()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }
    private fun setItemData(data:List<Item>,view:View){
        if (data.size>2){

        }else if(data.size==2){

        }else{

        }
    }
    private inner class MyAdapter:RecyclerView.Adapter<MyHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val view= LayoutInflater.from(parent.context).inflate(R.layout.rvitem_brand_special,parent,false)
            return MyHolder(view)
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            if (dataList.isNotEmpty()){
                val dataBean=dataList[position]
                LoadUtils().loadCommonImag(context!!,holder.ivShopLogo!!,dataBean.brand_logo?:"",R.drawable.ic_good_place_holder)
                holder.tvShopName!!.text=dataBean.tb_brand_name?:""
                holder.tvShoptitle!!.text=dataBean.introduce?:""
                if (dataBean.item!=null&&dataBean.item!!.isNotEmpty()){
                    val itemBean=dataBean.item!![0]
                    val shopType=itemBean.shoptype?:"A"
                    val ppType=itemBean.shopname?:"旗舰店"
                    if ("B"==shopType){  //天猫
                        holder.ivShopType!!.setImageResource(R.drawable.imag_shop_tm)
                    }else{  //淘宝
                        holder.ivShopType!!.setImageResource(R.drawable.imag_shop_tb)
                    }
                    if (ppType.contains("旗舰店")){  //旗舰店
                        holder.ivPPType!!.setImageResource(R.drawable.imag_shop_qjd)
                    }else{  //品牌店
                        holder.ivPPType!!.setImageResource(R.drawable.imag_shop_pp)
                    }
                    if (dataBean.item!!.size>2){
                        holder.clGoods1!!.visibility=View.VISIBLE
                        holder.clGoods2!!.visibility=View.VISIBLE
                        holder.clGoods3!!.visibility=View.VISIBLE
                        val itemBean1=dataBean.item!![1]
                        val itemBean2=dataBean.item!![2]
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods1!!,itemBean.itempic?:"",R.drawable.ic_good_place_holder,5)
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods2!!,itemBean1.itempic?:"",R.drawable.ic_good_place_holder,5)
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods3!!,itemBean2.itempic?:"",R.drawable.ic_good_place_holder,5)
                        holder.tvGoddsTitle1!!.text=itemBean.itemtitle?:""
                        holder.tvGoddsTitle2!!.text=itemBean1.itemtitle?:""
                        holder.tvGoddsTitle3!!.text=itemBean2.itemtitle?:""
                        holder.tvYHQPrice1!!.text="券 ￥"+itemBean.couponmoney?:"0"
                        holder.tvYHQPrice2!!.text="券 ￥"+itemBean1.couponmoney?:"0"
                        holder.tvYHQPrice3!!.text="券 ￥"+itemBean2.couponmoney?:"0"
                        holder.tvPrice1!!.text=itemBean.itemendprice?:"0"
                        holder.tvPrice2!!.text=itemBean1.itemendprice?:"0"
                        holder.tvPrice3!!.text=itemBean2.itemendprice?:"0"
                    }else if(dataBean.item!!.size==2){
                        holder.clGoods1!!.visibility=View.VISIBLE
                        holder.clGoods2!!.visibility=View.VISIBLE
                        holder.clGoods3!!.visibility=View.GONE
                        val itemBean1=dataBean.item!![1]
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods1!!,itemBean.itempic?:"",R.drawable.ic_good_place_holder,5)
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods2!!,itemBean1.itempic?:"",R.drawable.ic_good_place_holder,5)
                        holder.tvGoddsTitle1!!.text=itemBean.itemtitle?:""
                        holder.tvGoddsTitle2!!.text=itemBean1.itemtitle?:""
                        holder.tvYHQPrice1!!.text="券 ￥"+itemBean.couponmoney?:"0"
                        holder.tvYHQPrice2!!.text="券 ￥"+itemBean1.couponmoney?:"0"
                        holder.tvPrice1!!.text=itemBean.itemendprice?:"0"
                        holder.tvPrice2!!.text=itemBean1.itemendprice?:"0"
                    }else{
                        holder.clGoods1!!.visibility=View.VISIBLE
                        holder.clGoods2!!.visibility=View.GONE
                        holder.clGoods3!!.visibility=View.GONE
                        LoadUtils().loadRoundImag(context!!,holder.ivGoods1!!,itemBean.itempic?:"",R.drawable.ic_good_place_holder,5)
                        holder.tvGoddsTitle1!!.text=itemBean.itemtitle?:""
                        holder.tvYHQPrice1!!.text="券 ￥"+itemBean.couponmoney?:"0"
                        holder.tvPrice1!!.text=itemBean.itemendprice?:"0"
                    }
                }else{
                    holder.clGoods1!!.visibility=View.GONE
                    holder.clGoods2!!.visibility=View.GONE
                    holder.clGoods3!!.visibility=View.GONE
                }
                holder.clGoods1!!.setOnClickListener {
                    if (dataBean.item!=null&&dataBean.item!!.isNotEmpty()){
                        val itemBean=dataBean.item!![0]
                        val goodBean=TaobaoGoodBean()
                        goodBean.item_id=itemBean.itemid?:""
                        goodBean.goodsWenAn=itemBean.itemdesc?:""
                        goodBean.volume="0"
                        goodBean.goodsSource1="好单库"
                        goodBean.small_images=""
                        goodBean.pict_url=""
                        val intent = Intent(context, GoodDetailActivity::class.java)
                        intent.putExtra("good", goodBean)
                        startActivity(intent)
                    }
                }
                holder.clGoods2!!.setOnClickListener {
                    if (dataBean.item!=null&&dataBean.item!!.size>1){
                        val itemBean=dataBean.item!![1]
                        val goodBean=TaobaoGoodBean()
                        goodBean.item_id=itemBean.itemid?:""
                        goodBean.goodsWenAn=itemBean.itemdesc?:""
                        goodBean.volume="0"
                        goodBean.goodsSource1="好单库"
                        goodBean.small_images=""
                        goodBean.pict_url=""
                        val intent = Intent(context, GoodDetailActivity::class.java)
                        intent.putExtra("good", goodBean)
                        startActivity(intent)
                    }
                }
                holder.clGoods3!!.setOnClickListener {
                    if (dataBean.item!=null&&dataBean.item!!.size>2){
                        val itemBean=dataBean.item!![2]
                        val goodBean=TaobaoGoodBean()
                        goodBean.item_id=itemBean.itemid?:""
                        goodBean.goodsWenAn=itemBean.itemdesc?:""
                        goodBean.volume="0"
                        goodBean.goodsSource1="好单库"
                        goodBean.small_images=""
                        goodBean.pict_url=""
                        val intent = Intent(context, GoodDetailActivity::class.java)
                        intent.putExtra("good", goodBean)
                        startActivity(intent)
                    }
                }
                holder.itemView.setOnClickListener {
                    val intent=Intent(holder.itemView.context,BrandSpecialListActivity::class.java)
                    intent.putExtra("Name",dataBean.tb_brand_name?:"")
                    intent.putExtra("Id",dataBean.id?:"")
                    startActivity(intent)
                }
            }
        }

    }
    private inner class MyHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var ivShopLogo:ImageView?=null
        var tvShopName:TextView?=null
        var ivShopType:ImageView?=null
        var ivPPType:ImageView?=null
        var tvShoptitle:TextView?=null
        var clGoods1:ConstraintLayout?=null
        var ivGoods1:ImageView?=null
        var tvGoddsTitle1:TextView?=null
        var tvYHQPrice1:TextView?=null
        var tvPrice1:TextView?=null
        var clGoods2:ConstraintLayout?=null
        var ivGoods2:ImageView?=null
        var tvGoddsTitle2:TextView?=null
        var tvYHQPrice2:TextView?=null
        var tvPrice2:TextView?=null
        var clGoods3:ConstraintLayout?=null
        var ivGoods3:ImageView?=null
        var tvGoddsTitle3:TextView?=null
        var tvYHQPrice3:TextView?=null
        var tvPrice3:TextView?=null
        init {
            ivShopLogo=itemview.ivShopLogo
            tvShopName=itemview.tvShopName
            ivShopType=itemview.ivShopType
            ivPPType=itemview.ivPPType
            tvShoptitle=itemview.tvShopTitle
            clGoods1=itemview.clGoods1
            ivGoods1=itemview.ivGoods1
            tvGoddsTitle1=itemview.tvGoodsTitle1
            tvYHQPrice1=itemview.tvQuanPrice1
            tvPrice1=itemview.tvGoodsPrice1

            clGoods2=itemview.clGoods2
            ivGoods2=itemview.ivGoods2
            tvGoddsTitle2=itemview.tvGoodsTitle2
            tvYHQPrice2=itemview.tvQuanPrice2
            tvPrice2=itemview.tvGoodsPrice2

            clGoods3=itemview.clGoods3
            ivGoods3=itemview.ivGoods3
            tvGoddsTitle3=itemview.tvGoodsTitle3
            tvYHQPrice3=itemview.tvQuanPrice3
            tvPrice3=itemview.tvGoodsPrice3
        }
    }
}